/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.tech15.core.dao;


/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.tech15.core.model.Group;
import com.tech15.core.model.GroupUser;
import com.tech15.core.vo.query.GroupQuery;
import com.tech15.core.vo.query.GroupUserQuery;

@Repository
public class GroupDao extends BaseHibernateDao<Group,Long>{

	public Class getEntityClass() {
		return Group.class;
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		String sql = "select t from Group t where 1=1 "
				+ "/~ and t.groupname = '[groupname]' ~/"
				+ "/~ and t.memo = '[memo]' ~/"
				+ "/~ order by [sortColumns] ~/";
		return pageQuery(sql,pageRequest);
	}
	
	public List<Group> selectGroup(Integer isshfw) {
		String sql = "select t.* from sys_group t where t.isshfw="+isshfw;
		Map map = new HashMap();
        map.put("pgid", "Long");
        map.put("groupname", "String");
        map.put("remark", "String");
		return findSqlAll(sql, map, Group.class);
	}
	
	public List<Group> selectGroupByUserid(final Long userid) {
		return (List<Group>) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(final Session session) throws HibernateException, SQLException {
				String sql = "select a.pgid || '|' || 0 as selectUserIds,a.groupname  from sys_group a where a.pgid in (select pgid from sys_group_user t where t.userid="+userid+")";
				SQLQuery query = (SQLQuery)session.createSQLQuery(sql).setResultTransformer(new AliasToBeanResultTransformer(Group.class));
				query.addScalar("selectUserIds",Hibernate.STRING);
				query.addScalar("groupname",Hibernate.STRING);
				
				return query.list();
			}
		});
	}
	
	public List<Group> findAll(Integer appid) {
		return (List<Group>) findAllBy("appid", appid);
	}
	
	public Page findPage(GroupQuery query) {
		String sql = "select t.*,t1.appname from sys_group t left join pub_apps t1 on t.appid=t1.id where 1=1 "
			  	+ "/~ and t.groupname = '%[groupname]%' ~/"
			  	+ "/~ and t.memo like '%[groupname]%' ~/"
			  	+ "/~ and t.appid = [appid] ~/"
			  	+ "/~ and t.isshfw = [isshfw] ~/"
				+ "/~ order by [sortColumns] ~/";

		Map map = new HashMap();
        map.put("pgid", "Long");
        map.put("groupname", "String");
        map.put("remark", "String");
        map.put("appname", "String");
		return pageSqlQuery(sql,query,map,Group.class);
	}

	public List<GroupUser> selectGroupUsersByPgid(Long pgid) {
		String sql = "select a.*,b.realname from sys_group_user a left join sys_user b on a.userid=b.userid where a.pgid="+pgid;
		Map map = new HashMap();
        map.put("pguid", "Long");
        map.put("pgid", "Long");
        map.put("userid", "Long");
        map.put("realName", "String");
		return findSqlAll(sql, map, GroupUser.class);
	}

	public Page searchGroupUsers(GroupUserQuery query) {
		//,get_deptNameById(t.userid) as orgInfo
		String sql = "select t.*,b.username,b.realname from sys_group_user t,sys_user b where t.userid=b.userid  ";
		StringBuilder sql2 = new StringBuilder(sql);
		if(isNotEmpty(query.getPgid())) {
            sql2.append(" and  t.pgid = :pgid ");
        }
		Map map = new HashMap();
		map.put("pguid", "Long");
		map.put("pgid", "Long");
		map.put("userid", "Long");
		map.put("realName", "String");
		map.put("username", "String");
		//map.put("orgInfo", "String");
		return pageSqlQuery(sql2.toString(),query,map,GroupUser.class);
	}
	

}
